<template>
	<base-page>
		<view class="goodslist">
			<view class="goodslist-box">
				<view class="goodslist-left">
					<view class="goods-title">
						订单管理
						<text class="iconfont icongengduo1"></text>
						<!-- <view class="screen">
							筛选
							<text class="iconfont iconxiala"></text>
						</view> -->
					</view>

					<view class="goods-search">
						<view class="search">
							<text class="iconfont icon31sousuo"></text>
							<input type="text" v-model="search_text" @input="search"
								placeholder="搜索订单号/商品名称/手机号/收货人姓名" />
						</view>
					</view>
					<view class="order-type-list">
						<view class="class-item" :class="{ active: currOrderList == 'cashier' }"
							@click="selectOrderList('cashier')">收银订单</view>
						<view class="class-item" :class="{ active: currOrderList == 'online' }"
							@click="selectOrderList('online')">商城订单</view>
					</view>
					<block v-if="!one_judge && order_list.length > 0">
						<scroll-view scroll-y="true" class="goods-list-scroll" :show-scrollbar="false"
							@scrolltolower="getOrderList">
							<view class="item" @click="getOrderDetail(item.order_id, index)"
								v-for="(item, index) in order_list" :key="index"
								:class="index == selectGoodsKeys ? 'itemhover' : ''">
								<view class="title">
									<view>订单编号：{{ item.order_no }}</view>
									<view v-if="item.order_type == 5">{{ item.cashier_order_type_name }}</view>
									<view v-else="item.order_type != 5">{{ item.order_type_name }}</view>
								</view>
								<view class="total-money-num">
									<view>{{ item.order_status_name }}</view>
									<view class="member-info">
										<view>买家：</view>
										<view v-if="item.member_id">{{ item.nickname }}</view>
										<view v-else>散客</view>
									</view>

									<view class="box">
										<view>实付金额</view>
										<view>￥{{ item.pay_money }}</view>
									</view>
								</view>
							</view>
						</scroll-view>
					</block>
					<view class="notYet" v-else-if="!one_judge && order_list.length == 0">暂无数据</view>
				</view>
				<view class="goodslist-right" v-show="type == 'detail'">
					<view class="goods-title">订单详情</view>
					<view class="order-information tab-wrap" v-show="!one_judge">
						<view class="tab-head">
							<text v-for="(item, index) in tabObj.list" :key="index"
								:class="{ active: tabObj.index == item.value }"
								v-if="(item.value == 3 && order_detail.order_log && order_detail.order_log.length > 0) || item.value != 3"
								@click="tabObj.index = item.value">
								{{ item.name }}
							</text>
						</view>
						<view class="tab-content" v-if="JSON.stringify(order_detail) != '{}'">
							<view class="other-information" v-if="tabObj.index == 1">
								<view class="item-box">
									<view class="item">
										<view>买家：</view>
										<view v-if="order_detail.member_id">
											{{ order_detail.nickname }}
											<text class="look" @click="viewMember()">查看会员</text>
										</view>
										<view v-else>散客</view>
									</view>
									<view class="item">
										<view>卖家备注：</view>
										<view>{{ order_detail.remark || '无' }}</view>
									</view>
									<view class="item">
										<view>订单类型：</view>
										<view>{{ order_detail.order_type_name }}</view>
									</view>
									<view class="item"
										v-if="order_detail.order_type == 2 || order_detail.order_type == 3">
										<view v-if="order_detail.order_type == 2">买家预计上门时间：</view>
										<view v-if="order_detail.order_type == 3">买家要求送达时间：</view>
										<view>
											<block v-if="order_detail.buyer_ask_delivery_time != 0">
												{{
													order_detail.buyer_ask_delivery_time.indexOf('-') != -1
														? order_detail.buyer_ask_delivery_time
														: $util.timeFormat(order_detail.buyer_ask_delivery_time)
												}}
											</block>
											<block v-else>
												<text v-if="order_detail.order_type == 2">未选择上门时间</text>
												<text v-if="order_detail.order_type == 3">即时送达</text>
											</block>
										</view>
									</view>
									<view class="item" v-for="(item, index) in otherInformation" :key="index"
										v-if="item.mation">
										<view>{{ item.title }}</view>
										<view>{{ item.mation }}</view>
									</view>
									<view class="item" v-if="order_detail.order_scene == 'cashier'">
										<view>收银员：</view>
										<view>{{ order_detail.cashier_operator_name }}</view>
									</view>
									<view class="item">
										<view>商品总价：</view>
										<view>￥{{ order_detail.goods_money }}</view>
									</view>
									<view class="item">
										<view>店铺优惠：</view>
										<view>-￥{{ order_detail.promotion_money }}</view>
									</view>
									<view class="item">
										<view>订单减免：</view>
										<view>-￥{{ order_detail.reduction }}</view>
									</view>
									<view class="item">
										<view>优惠券：</view>
										<view>-￥{{ order_detail.coupon_money }}</view>
									</view>
									<view class="item">
										<view>积分抵扣：</view>
										<view>-￥{{ order_detail.point_money }}</view>
									</view>
									<view class="item" v-show="order_detail.online_money > 0">
										<view>线上支付：</view>
										<view>￥{{ order_detail.online_money | moneyFormat }}</view>
									</view>
									<view class="item" v-show="order_detail.cash > 0">
										<view>现金支付：</view>
										<view>￥{{ order_detail.cash | moneyFormat }}</view>
									</view>
									<view class="item" v-show="order_detail.cash_change > 0">
										<view>找零：</view>
										<view>-￥{{ order_detail.cash_change | moneyFormat }}</view>
									</view>
									<view class="item" v-show="parseFloat(order_detail.balance_money) > 0">
										<view>余额抵扣：</view>
										<view>￥{{ $util.moneyFormat(order_detail.balance_money) }}</view>
									</view>
									<view class="item" v-show="order_detail.own_wechatpay > 0">
										<view>个人微信收款：</view>
										<view>￥{{ order_detail.own_wechatpay | moneyFormat }}</view>
									</view>
									<view class="item" v-show="order_detail.own_alipay > 0">
										<view>个人支付宝收款：</view>
										<view>￥{{ order_detail.own_alipay | moneyFormat }}</view>
									</view>
									<view class="item" v-show="order_detail.own_pos > 0">
										<view>个人POS收款：</view>
										<view>￥{{ order_detail.own_pos | moneyFormat }}</view>
									</view>
									<view class="item">
										<view>实付金额：</view>
										<view>￥{{ order_detail.pay_money }}</view>
									</view>
									<view class="item-box" v-if="order_detail.form">
										<view class="item" v-for="(item, index) in order_detail.form" :key="index">
											<view>{{ item.value.title }}：</view>
											<view class="img-box" v-if="item.controller == 'Img'">
												<view class="img" v-for="(img, imgIndex) in item.img_lists"
													:key="imgIndex">
													<image :src="$util.img(img)" mode="widthFix"
														@click="previewMedia($util.img(img))"></image>
												</view>
											</view>
											<view v-else>{{ item.val }}</view>
										</view>
									</view>
								</view>
							</view>

							<view class="goods-info" v-if="tabObj.index == 2">
								<view class="table">
									<view class="table-th table-all">
										<view class="table-td" style="width:55%">商品（元）</view>
										<view class="table-td" style="width:15%">价格</view>
										<view class="table-td" style="width:10%">数量</view>
										<view class="table-td" style="width:15%;justify-content: flex-end;">小计（元）</view>
									</view>
									<view class="table-tr table-all" v-for="(item, index) in order_detail.order_goods"
										:key="index">
										<view class="table-td" style="width:55%">
											<image
												:src="item.sku_image ? $util.img(item.sku_image, { size: 'small' }) : $util.img('public/uniapp/default_img/goods.png')"
												@error="item.sku_image = $util.img('public/uniapp/default_img/goods.png')"
												mode="widthFix"></image>
											<view class="content-text">
												<view>
													<text v-if="item.is_gift" class="gift-tag">赠品</text>
													{{ item.goods_name }} {{ item.spec_name }}
												</view>
												<view v-if="item.refund_status != 0">
													<text class="refun-status">{{ item.refund_status_name }}</text>
												</view>
											</view>
										</view>
										<view class="table-td" style="width:15%">{{ item.price }}</view>
										<view class="table-td" style="width:10%">{{ item.num }}</view>
										<view class="table-td uni-column" style="width:15%;align-items: flex-end;">
											<view>{{ item.goods_money }}</view>
											<view class="refund-success" v-if="item.refund_status == 'refund_complete'">
												退款成功</view>
										</view>
									</view>
								</view>
							</view>

							<view class="other-information journal"
								v-if="order_detail.order_log && order_detail.order_log.length > 0 && tabObj.index == 3">
								<nsjournal :list="order_detail.order_log"></nsjournal>
							</view>


						</view>
						<nc-loading ref="detailLoading"></nc-loading>
					</view>
					<view class="remarks-box" v-if="JSON.stringify(order_detail) != '{}'">
						<button type="primary" class="default-btn comp-btn remarks" @click="printTicket">打印小票</button>
						<button type="primary" class="default-btn comp-btn remarks" @click="type = 'refund'"
							v-if="order_detail.is_enable_refund">退款</button>
						<button type="primary" class="default-btn comp-btn remarks" @click="open('remark')">备注</button>
						<block v-if="order_detail.order_status_action.action">
							<block v-for="(item, index) in order_detail.order_status_action.action" :key="index">
								<button type="primary" class="primary-btn btn remarks" @click="open(item['action'])"
									v-if="item['action'] == 'orderLocalDelivery' || item['action'] == 'orderDelivery' || item['action'] == 'orderClose'">
									{{ item.title }}
								</button>
							</block>
						</block>
						<block v-if="order_detail.order_type == 2 && order_detail.order_status == 2">
							<button type="primary" class="primary-btn btn remarks"
								@click="open('storeOrderTakedelivery')">提货</button>
						</block>
						<button v-for="(item, index) in order_detail.order_action" :key="index" type="primary"
							class="primary-btn btn remarks" @click="open(item['action'])">
							{{ item.title }}
						</button>
					</view>
				</view>
				<!-- 订单退款 -->
				<view class="goodslist-right refund-wrap" v-show="type == 'refund'">
					<view class="goods-title">退款</view>
					<view class="content common-scrollbar">
						<view v-show="refundStep == 0">
							<block v-for="(item, index) in order_detail.order_goods" :key="index">
								<view class="goods-item" v-if="item.refund_status == 0 && item.goods_class != 'money'">
									<view class="iconfont"
										:class="refundGoods.indexOf(item.order_goods_id) == -1 ? 'iconyuan_checkbox' : 'iconyuan_checked'"
										@click="selectOrderGoods(item)"></view>
									<view class="image">
										<image
											:src="item.sku_image ? $util.img(item.sku_image, { size: 'small' }) : $util.img('public/uniapp/default_img/goods.png')"
											@error="item.sku_image = $util.img('public/uniapp/default_img/goods.png')"
											mode="widthFix"></image>
									</view>
									<view class="info">
										<view class="content-text">{{ item.goods_name }}</view>
									</view>
									<view>
										<view class="price">￥{{ item.price }}</view>
										<view class="num">x {{ item.num }}</view>
									</view>
								</view>
							</block>
						</view>
						<block v-if="refundDetail">
							<view class="bg-grey" v-show="refundStep == 1">
								<block v-for="(refundItem, refundIndex) in refundDetail.refund_list" :key="refundIndex">
									<view class="refund-goods-item">
										<view class="row">
											<view class="title">退款商品</view>
											<view class="cont">
												<view class="goods-item">
													<view class="image">
														<image
															:src="$util.img(refundItem.order_goods_info.sku_image, { size: 'small' })"
															@error="refundItem.order_goods_info.sku_image = $util.img('public/uniapp/default_img/goods.png')"
															mode="widthFix"></image>
													</view>
													<view class="info">
														<view class="content-text">
															{{ refundItem.order_goods_info.sku_name }}
														</view>
													</view>
												</view>
											</view>
										</view>
										<view class="row">
											<view class="title">退款金额</view>
											<view class="cont">
												<view class="money-box">
													<input type="number"
														v-model="refundApply.refund_array[refundItem.order_goods_info.order_goods_id].refund_money" />
													元
												</view>
												<view class="refund-money">
													可退金额：
													<text>￥{{ refundItem.order_goods_info.refund_apply_money | moneyFormat }}</text>
												</view>
												<view class="refund-money" v-if="refundItem.order_goods_info.use_point">
													退还积分：
													<text>{{ refundItem.order_goods_info.use_point }}积分</text>
												</view>
											</view>
										</view>
									</view>
								</block>

								<view class="refund-goods-item">
									<view class="row">
										<view class="title">退款说明</view>
										<view class="cont"><textarea value="" placeholder="请输入退款说明"
												v-model="refundApply.refund_remark" /></view>
									</view>
								</view>
							</view>
							<view v-show="refundStep == 2">
								<view class="refund-type" :class="{ active: refundApply.refund_transfer_type == index }"
									@click="refundApply.refund_transfer_type = index"
									v-for="(item, index) in refundDetail.refund_transfer_type" :key="index">
									<view class="title">{{ item.name }}</view>
									<view class="desc">{{ item.desc }}</view>
								</view>
							</view>
						</block>
					</view>
					<view class="remarks-box">
						<button type="primary" class="default-btn comp-btn remarks" @click="type = 'detail'">取消</button>
						<button type="primary" class="primary-btn comp-btn remarks" v-show="refundStep > 0"
							@click="refundStep -= 1">上一步</button>
						<button type="primary" class="primary-btn comp-btn remarks"
							@click="refundNext">{{ refundStep == 2 ? '确认退款' : '下一步' }}</button>
					</view>
				</view>
			</view>

			<!-- 留言 -->
			<unipopup ref="remark" type="center"><ns-remark :message="order_detail.remark" ref="remark" @close="close"
					@remark="remark"></ns-remark></unipopup>

			<!-- 订单关闭 -->
			<unipopup ref="orderClose" type="center">
				<view class="order-clsoe">
					<view class="title">是否要关闭订单？</view>
					<view class="btn">
						<button type="primary" class="default-btn btn save" @click="orderoperation('close')">取消</button>
						<button type="primary" class="primary-btn btn" @click="orderoperation('save')">确定</button>
					</view>
				</view>
			</unipopup>
			<!-- 门店提货 -->
			<unipopup ref="storeOrderTakedelivery" type="center">
				<view class="order-clsoe">
					<view class="title">确定要直接提货吗？</view>
					<view class="btn">
						<button type="primary" class="default-btn btn save"
							@click="$refs.storeOrderTakedelivery.close()">取消</button>
						<button type="primary" class="primary-btn btn" @click="storeOrderTakedelivery()">确定</button>
					</view>
				</view>
			</unipopup>

			<unipopup ref="orderLocalDelivery" type="center">
				<view class="order-delivery local">
					<view class="title">订单发货</view>
					<view class="content">
						<view class="content-item">
							<view class="title">收货地址：</view>
							<view class="info">
								<text>{{ order_detail.name }}</text>
								<text>{{ order_detail.mobile }}</text>
								<text>{{ order_detail.full_address }}{{ order_detail.address }}</text>
							</view>
						</view>
						<view class="content-item">
							<view class="title">配送员：</view>
							<view class="info">
								<view class="select">
									<select-lay :zindex="10" :value="localDelivery.deliverer" name="names"
										placeholder="请选择配送员" :options="deliverer"
										@selectitem="selectDeliverer"></select-lay>
								</view>
							</view>
						</view>
						<view class="content-item">
							<view class="title">配送员手机号：</view>
							<view class="info"><input type="text" class="input"
									v-model="localDelivery.deliverer_mobile" /></view>
						</view>
					</view>
					<view class="btn">
						<button type="primary" class="default-btn btn save"
							@click="$refs.orderLocalDelivery.close()">取消</button>
						<button type="primary" class="primary-btn btn" @click="orderLocalDelivery()">确定</button>
					</view>
				</view>
			</unipopup>

			<unipopup ref="orderDelivery" type="center">
				<view class="order-delivery express">
					<view class="title">订单发货</view>
					<view class="content">
						<view class="content-item">
							<view class="title">收货地址：</view>
							<view class="info">
								<text>{{ order_detail.name }}</text>
								<text>{{ order_detail.mobile }}</text>
								<text>{{ order_detail.full_address }}{{ order_detail.address }}</text>
							</view>
						</view>
						<view class="content-item">
							<view class="title">发货方式：</view>
							<view class="info">
								<radio-group @change="expresDelivery.delivery_type = $event.detail.value"
									class="form-radio-group">
									<label class="radio form-radio-item">
										<radio value="1" :checked="expresDelivery.delivery_type == 1" />物流发货
									</label>
									<label class="radio form-radio-item">
										<radio value="0" :checked="expresDelivery.delivery_type == 0" />无需物流
									</label>
								</radio-group>
							</view>
						</view>
						<view v-show="expresDelivery.delivery_type == 1">
							<view class="content-item">
								<view class="title">物流公司：</view>
								<view class="info">
									<view class="select">
										<select-lay :zindex="10" :value="expresDelivery.express_company_id" name="names"
											placeholder="请选择物流公司" :options="expressCompany"
											@selectitem="selectExpressCompany"></select-lay>
									</view>
								</view>
							</view>
							<view class="content-item">
								<view class="title">快递单号：</view>
								<view class="info"><input type="text" class="input"
										v-model="expresDelivery.delivery_no" /></view>
							</view>
						</view>
						<view class="content-item">
							<view class="title"></view>
							<view class="info goods-info common-scrollbar">
								<view class="table">
									<checkbox-group @change="expresDelivery.order_goods_ids = $event.detail.value">
										<view class="table-th table-all">
											<view class="table-td" style="width:5%"></view>
											<view class="table-td" style="width:45%">商品（元）</view>
											<view class="table-td" style="width:10%">数量</view>
											<view class="table-td" style="width:20%;justify-content: flex-end;">物流单号
											</view>
											<view class="table-td" style="width:20%;justify-content: flex-end;">物流状态
											</view>
										</view>
										<view class="table-tr table-all"
											v-for="(item, index) in order_detail.order_goods" :key="index">
											<view class="table-td" style="width:5%">
												<checkbox :value="item.order_goods_id"
													:disabled="item.delivery_status != 0" />
											</view>
											<view class="table-td" style="width:45%">
												<image
													:src="item.sku_image ? $util.img(item.sku_image, { size: 'small' }) : $util.img('public/uniapp/default_img/goods.png')"
													@error="item.sku_image = $util.img('public/uniapp/default_img/goods.png')"
													mode="widthFix"></image>
												<view class="content-text">
													<view>
														{{ item.goods_name }} {{ item.spec_name }}
													</view>
												</view>
											</view>
											<view class="table-td" style="width:10%">{{ item.num }}</view>
											<view class="table-td uni-column" style="width:20%;align-items: flex-end;">
												{{ item.delivery_no }}
											</view>
											<view class="table-td uni-column" style="width:20%;align-items: flex-end;">
												{{ item.delivery_status_name }}
											</view>
										</view>
									</checkbox-group>
								</view>
							</view>
						</view>
					</view>
					<view class="btn">
						<button type="primary" class="default-btn btn save"
							@click="$refs.orderDelivery.close()">取消</button>
						<button type="primary" class="primary-btn btn" @click="orderDelivery()">确定</button>
					</view>
				</view>
			</unipopup>

			<!-- 订单调价 -->
			<unipopup ref="orderAdjustMoney" type="center">
				<nsjustmoney v-if="order_detail && order_detail.order_goods && order_detail.order_goods.length > 0"
					:goods_list="JSON.parse(JSON.stringify(order_detail.order_goods))"
					:pay_money="order_detail.pay_money" :discount_money="order_detail.discount_money"
					:offset_money="order_detail.offset_money" @orderAdjustMoney="orderAdjustMoney" @close="close">
				</nsjustmoney>
			</unipopup>

			<!-- 订单删除 -->
			<unipopup ref="orderDelete" type="center">
				<view class="order-clsoe">
					<view class="title">是否要删除订单？</view>
					<view class="btn">
						<button type="primary" class="default-btn btn save" @click="orderdelete('close')">取消</button>
						<button type="primary" class="primary-btn btn" @click="orderdelete('save')">确定</button>
					</view>
				</view>
			</unipopup>
		</view>
	</base-page>
</template>

<script>
	import nsjournal from '@/components/ns-journal/ns-journal.vue';
	import unipopup from '@/components/uni-popup/uni-popup.vue';
	import nsremark from '@/components/ns-remark/ns-remark.vue';
	import nsjustmoney from '@/components/ns-justmoney/ns-justmoney.vue';
	export default {
		components: {
			nsjournal,
			nsremark,
			unipopup,
			nsjustmoney
		},
		data() {
			return {
				selectGoodsKeys: 0,
				otherInformation: {
					order_no: {
						title: '订单编号：',
						mation: ''
					},
					out_trade_no: {
						title: '订单交易号：',
						mation: ''
					},
					create_time: {
						title: '创建日期：',
						mation: ''
					},
					sell_time: {
						title: '消费日期：',
						mation: ''
					},
					pay_status: {
						title: '支付状态：',
						mation: ''
					},
					order_status: {
						title: '订单状态：',
						mation: ''
					},
					pay_type: {
						title: '付款方式：',
						mation: ''
					},
					operator_name: {
						title: '收银员：',
						mation: ''
					},
					pay_time: {
						title: '付款时间：',
						mation: ''
					}
				},
				// 订购日志所需列表数据
				list: [],
				//获取订单的页数
				page: 1,
				//每次获取订单的条数
				page_size: 8,
				// 订单搜索是用到的数据
				search_text: '',
				//订单列表类型
				currOrderList: 'cashier',
				//订单类型
				trade_type: '',
				//初始时加载详情数据判断
				one_judge: true,
				// 订单列表数据
				order_list: [],
				//订单详情数据
				order_detail: {},
				type: 'detail',
				refundStep: 0,
				refundGoods: [],
				refundDetail: null,
				refundRepeat: false,
				refundApply: {
					refund_remark: '',
					refund_transfer_type: ''
				},
				localDelivery: {
					deliverer_mobile: '',
					deliverer: ''
				},
				expresDelivery: {
					delivery_type: 1,
					express_company_id: 0,
					delivery_no: '',
					order_goods_ids: []
				},
				expressCompany: [],
				deliverer: [],
				tabObj: {
					list: [{
							value: 1,
							name: '基础信息'
						},
						{
							value: 2,
							name: '商品信息'
						},
						{
							value: 3,
							name: '订单日志'
						}
					],
					index: 1
				}
			};
		},
		onLoad(option) {
			this.order_type = option.order_type || '';
			/**
			 * 获取订单列表数据
			 */
			this.getOrderList();
			this.getExpressCompany()
			this.getDeliver()
		},
		watch: {
			type: function(nval, oval) {
				if (oval == 'refund') {
					this.refundStep = 0;
					this.refundGoods = [];
					this.refundDetail = null;
					this.refundRepeat = false;
					this.refundApply = {
						refund_remark: '',
						refund_transfer_type: ''
					};
				}
			}
		},
		methods: {
			switchStoreAfter() {
				this.page = 1;
				this.order_list = [];
				this.one_judge = true;
				this.getOrderList();
				this.getDeliver()
			},
			// 搜索
			search() {
				this.page = 1;
				this.order_list = [];
				this.one_judge = true;
				this.getOrderList();
			},
			selectOrderList(order_type) {
				this.currOrderList = order_type;
				this.page = 1;
				this.order_list = [];
				this.one_judge = true;
				this.selectGoodsKeys = 0;
				this.getOrderList();
			},
			/**
			 * 获取订单列表
			 */
			getOrderList() {
				this.$api.sendRequest({
					url: '/cashier/storeapi/cashierorder/lists',
					data: {
						page: this.page,
						page_size: this.page_size,
						search_text: this.search_text,
						order_scene: this.currOrderList
					},
					success: res => {
						if (res.data.list.length == 0 && this.one_judge) {
							this.order_detail = {};
							this.one_judge = false;
						}
						if (res.code >= 0 && res.data.list.length != 0) {
							this.page += 1;

							if (this.order_list.length == 0) {
								this.order_list = res.data.list;
							} else {
								this.order_list = this.order_list.concat(res.data.list);
							}

							//初始时加载一遍详情数据
							if (this.one_judge) {
								this.getOrderDetail(this.order_list[0].order_id);
							}
						}
					}
				});
			},

			/**
			 * 获取订单详情数据
			 */
			getOrderDetail(order_id, keys = 0, callback) {
				this.selectGoodsKeys = keys;
				this.type = 'detail';
				this.$refs.detailLoading.show()
				this.$api.sendRequest({
					url: '/cashier/storeapi/Cashierorder/detail',
					data: {
						order_id
					},
					success: res => {
						if (res.code >= 0) {
							this.order_detail = res.data;
							this.order_detail.order_status_action = JSON.parse(res.data.order_status_action)
							this.otherInformation.order_no.mation = res.data.order_no;
							this.otherInformation.out_trade_no.mation = res.data.out_trade_no;
							this.otherInformation.create_time.mation = this.$util.timeFormat(res.data
								.create_time);
							this.otherInformation.sell_time.mation = res.data.sell_time ? this.$util
								.timeFormat(res.data.sell_time) : this.$util.timeFormat(res.data.create_time);
							this.otherInformation.operator_name.mation = res.data.operator_name;
							this.otherInformation.pay_type.mation = res.data.pay_type_name;
							this.otherInformation.order_status.mation = res.data.order_status_name;
							if (res.data.pay_status == 1) {
								this.otherInformation.pay_status.mation = '已支付';
								this.otherInformation.pay_time.mation = this.$util.timeFormat(res.data
									.pay_time);
							} else {
								this.otherInformation.pay_status.mation = '待支付';
								this.otherInformation.pay_time.mation = '';
							}
							if (typeof callback == 'function') {
								callback();
							}
							this.$forceUpdate();
							this.one_judge = false;
							if (this.$refs.detailLoading) this.$refs.detailLoading.hide()
						}
					}
				});
			},

			/**
			 * @param {Object} 订单功能 弹窗开启
			 */
			open(action) {
				this.$refs[action].open();
			},
			/**
			 * @param {Object} 订单操作组弹窗关闭
			 */
			close(name) {
				this.$refs[name].close();
			},

			/**
			 * 留言数据保存
			 */
			remark(text, callback) {
				this.$api.sendRequest({
					url: '/cashier/storeapi/cashierorder/orderRemark',
					data: {
						order_id: this.order_detail.order_id,
						remark: text
					},
					success: res => {
						this.$util.showToast({
							title: res.message
						});
						if (typeof callback == 'function') {
							callback();
						}
						if (res.code >= 0) {
							this.getOrderDetail(this.order_list[this.selectGoodsKeys].order_id);
							this.$refs.remark.close();
						}
					}
				});
			},

			/**
			 * 关闭订单
			 */
			orderClose() {
				this.$api.sendRequest({
					url: '/cashier/storeapi/order/close',
					data: {
						order_id: this.order_detail.order_id
					},
					success: res => {
						this.$util.showToast({
							title: res.message
						});
						if (res.code >= 0) {
							this.getOrderDetail(this.order_list[this.selectGoodsKeys].order_id, this
								.selectGoodsKeys, () => {
									this.order_list[this.selectGoodsKeys] = this.order_detail;
									this.$forceUpdate();
								});
							this.$refs.orderClose.close();
						}
					}
				});
			},

			/**
			 * 调整价格
			 */
			orderAdjustMoney(data, callback) {
				this.$api.sendRequest({
					url: '/cashier/storeapi/order/adjustPrice',
					data: {
						order_id: this.order_detail.order_id,
						adjust_money: data.adjust_money
					},
					success: res => {
						this.$util.showToast({
							title: res.message
						});
						if (typeof callback == 'function') {
							callback();
						}
						if (res.code >= 0) {
							this.getOrderDetail(this.order_list[this.selectGoodsKeys].order_id, this
								.selectGoodsKeys);
							this.$refs.orderAdjustMoney.close();
						}
					}
				});
			},

			/**
			 * 订单删除
			 */
			orderDelete() {
				this.$api.sendRequest({
					url: '/cashier/storeapi/order/delete',
					data: {
						order_id: this.order_detail.order_id
					},
					success: res => {
						this.$util.showToast({
							title: res.message
						});
						if (res.code >= 0) {
							this.order_list.splice(this.selectGoodsKeys, 1);

							if (this.selectGoodsKeys == 0) {
								this.selectGoodsKeys = 0;
							} else {
								this.selectGoodsKeys -= 1;
							}

							this.getOrderDetail(this.order_list[this.selectGoodsKeys].order_id, this
								.selectGoodsKeys);
							this.$refs.orderDelete.close();
							this.$forceUpdate();
						}
					}
				});
			},

			/**
			 * 订单取消
			 */
			orderoperation(type) {
				switch (type) {
					case 'save':
						this.orderClose();
						break;
					case 'close':
						this.$refs.orderClose.close();
						break;
				}
			},

			/**
			 * 订单数删除操作
			 */
			orderdelete(type) {
				switch (type) {
					case 'save':
						this.orderDelete();
						break;
					case 'close':
						this.$refs.orderDelete.close();
						break;
				}
			},
			selectOrderGoods(data) {
				let index = this.refundGoods.indexOf(data.order_goods_id);
				if (index == -1) this.refundGoods.push(data.order_goods_id);
				else this.refundGoods.splice(index, 1);
			},
			/**
			 * 退款下一步
			 */
			refundNext() {
				if (this.refundStep == 0) {
					if (!this.refundGoods.length) {
						this.$util.showToast({
							title: '请选择要退款的商品'
						});
						return;
					}

					this.$api.sendRequest({
						url: '/cashier/storeapi/cashierorderrefund/getrefundapplydata',
						data: {
							refund_array: JSON.stringify(this.refundGoods)
						},
						success: res => {
							if (res.code == 0) {
								this.refundDetail = res.data;
								this.refundStep = 1;
								let refundData = {};

								this.refundDetail.refund_list.forEach(refundItem => {
									refundData[refundItem.order_goods_info.order_goods_id] = {
										refund_pay_money: this.$util.moneyFormat(refundItem
											.order_goods_info.refund_apply_money),
										refund_money: this.$util.moneyFormat(refundItem
											.order_goods_info.refund_apply_money)
									};
								});
								Object.assign(this.refundApply, {
									order_id: this.order_detail.order_id,
									refund_array: refundData,
									refund_transfer_type: Object.keys(this.refundDetail
										.refund_transfer_type)[0]
								});
							} else {
								this.$util.showToast({
									title: res.message
								});
							}
						}
					});
				} else if (this.refundStep == 1) {
					if (this.refundVerify()) this.refundStep = 2;
				} else if (this.refundStep == 2) {
					this.createRefund();
				}
			},
			/**
			 * 退款验证
			 */
			refundVerify() {
				try {
					this.refundDetail.refund_list.forEach(refundItem => {
						let data = this.refundApply.refund_array[refundItem.order_goods_info.order_goods_id];
						if (isNaN(parseFloat(data.refund_money))) {
							this.$util.showToast({
								title: '退款金额输入错误'
							});
							throw new Error('');
						}
						if (parseFloat(data.refund_money) < 0) {
							this.$util.showToast({
								title: '退款金额不能小于0'
							});
							throw new Error('');
						}
						if (parseFloat(data.refund_money) > parseFloat(data.refund_pay_money)) {
							this.$util.showToast({
								title: '退款金额超出可退金额'
							});
							throw new Error('');
						}
					});
				} catch (e) {
					return false;
				}
				return true;
			},
			/**
			 * 退款申请
			 */
			createRefund() {
				if (this.refundRepeat) return;
				this.refundRepeat = true;

				uni.showLoading({
					title: ''
				});

				let data = this.$util.deepClone(this.refundApply);
				data.refund_array = JSON.stringify(data.refund_array);

				this.$api.sendRequest({
					url: '/cashier/storeapi/cashierorderrefund/refund',
					data,
					success: res => {
						uni.hideLoading();
						if (res.code == 0) {
							this.$util.showToast({
								title: '退款成功'
							});
							this.getOrderDetail(this.order_detail.order_id);
							this.type = 'detail';
						} else {
							this.refundRepeat = false;
							this.$util.showToast({
								title: res.message
							});
						}
					}
				});
			},
			/**
			 * 提货
			 */
			storeOrderTakedelivery() {
				if (this.isRepeat) return;
				this.isRepeat = true;

				uni.showLoading({
					title: ''
				});

				this.$api.sendRequest({
					url: '/cashier/storeapi/order/storedelivery',
					data: {
						order_id: this.order_detail.order_id
					},
					success: res => {
						uni.hideLoading();
						if (res.code == 0) {
							this.isRepeat = false;
							this.getOrderDetail(this.order_detail.order_id);
							this.$refs.storeOrderTakedelivery.close();
						} else {
							this.isRepeat = false;
							this.$util.showToast({
								title: res.message
							});
						}
					}
				});
			},
			/**
			 * 本地配送
			 */
			orderLocalDelivery() {
				if (!this.localDelivery.deliverer) {
					this.$util.showToast({
						title: '请选择配送员'
					});
					return;
				}
				if (!this.localDelivery.deliverer_mobile) {
					this.$util.showToast({
						title: '请输入配送员联系方式'
					});
					return;
				}

				if (this.isRepeat) return;
				this.isRepeat = true;

				uni.showLoading({
					title: ''
				});

				this.$api.sendRequest({
					url: '/cashier/storeapi/order/localdelivery',
					data: {
						order_id: this.order_detail.order_id,
						deliverer: this.localDelivery.deliverer,
						deliverer_mobile: this.localDelivery.deliverer_mobile
					},
					success: res => {
						uni.hideLoading();
						if (res.code == 0) {
							this.isRepeat = false;
							this.getOrderDetail(this.order_detail.order_id);
							this.localDelivery = {
								deliverer_mobile: '',
								deliverer: ''
							};
							this.$refs.orderLocalDelivery.close();
						} else {
							this.isRepeat = false;
							this.$util.showToast({
								title: res.message
							});
						}
					}
				});
			},
			getDeliver() {
				this.$api.sendRequest({
					url: '/cashier/storeapi/order/deliverlist',
					success: res => {
						if (res.code == 0 && res.data) {
							this.deliverer = res.data.map(item => {
								return {
									label: item.deliver_name,
									value: item.deliver_name, // 废弃，deliver_id
									mobile: item.deliver_mobile
								};
							});
						}
					}
				});
			},
			selectDeliverer(index, item) {
				if (index >= 0) {
					this.localDelivery.deliverer_mobile = this.deliverer[index].mobile; // 配送员手机号
					this.localDelivery.deliverer = item.value; // 配送员
				} else {
					this.localDelivery.deliverer_mobile = '';
					this.localDelivery.deliverer = '';
				}
			},
			viewMember() {
				this.$util.redirectTo('/pages/member/list');
				uni.setStorageSync('member_id', this.order_detail.member_id);
			},
			/**
			 * 打印小票
			 */
			printTicket() {
				this.$api.sendRequest({
					url: '/cashier/storeapi/cashierorder/printticket',
					data: {
						order_id: this.order_detail.order_id
					},
					success: res => {
						if (res.code == 0) {
							if (Object.values(res.data).length) {
								let data = Object.values(res.data)
								try {
									let print = {
										printer: []
									}
									data.forEach((item) => {
										print.printer.push({
											printer_type: item.printer_info.printer_type,
											host: item.printer_info.host,
											ip: item.printer_info.ip,
											port: item.printer_info.port,
											content: item.content,
                                            print_width: item.printer_info.print_width
										})
									})
									POS_.send('Print', JSON.stringify(print));
								} catch (e) {
									console.log('err', e, res.data)
								}
							} else {
								this.$util.showToast({
									title: '未开启订单小票打印'
								})
							}
						} else {
							this.$util.showToast({
								title: res.message ? res.message : '小票打印失败'
							})
						}
					}
				})
			},
			getExpressCompany() {
				this.$api.sendRequest({
					url: '/cashier/storeapi/order/expresscompany',
					success: res => {
						if (res.code == 0 && res.data) {
							this.expressCompany = res.data.map(item => {
								return {
									label: item.company_name,
									value: item.company_id
								};
							});
						}
					}
				});
			},
			selectExpressCompany(index, item) {
				if (index >= 0) {
					this.expresDelivery.express_company_id = parseInt(item.value)
				} else {
					this.expresDelivery.express_company_id = 0
				}
			},
			/**
			 * 物流配送订单发货
			 */
			orderDelivery() {
				if (this.expresDelivery.delivery_type == 1) {
					if (!this.expresDelivery.express_company_id) {
						this.$util.showToast({
							title: '请选择物流公司'
						});
						return;
					}
					if (!this.expresDelivery.delivery_no) {
						this.$util.showToast({
							title: '请输入物流单号'
						});
						return;
					}
				}
				if (!this.expresDelivery.order_goods_ids.length) {
					this.$util.showToast({
						title: '请选择要发货的商品'
					});
					return;
				}

				if (this.isRepeat) return;
				this.isRepeat = true;

				uni.showLoading({
					title: ''
				});

				this.$api.sendRequest({
					url: '/cashier/storeapi/order/expressdelivery',
					data: {
						order_id: this.order_detail.order_id,
						delivery_type: this.expresDelivery.delivery_type,
						express_company_id: this.expresDelivery.express_company_id,
						delivery_no: this.expresDelivery.delivery_no,
						order_goods_ids: this.expresDelivery.order_goods_ids.toString()
					},
					success: res => {
						uni.hideLoading();
						if (res.code == 0) {
							this.isRepeat = false;
							this.getOrderDetail(this.order_detail.order_id);
							this.expresDelivery = {
								delivery_type: 1,
								express_company_id: 0,
								delivery_no: '',
								order_goods_ids: []
							};
							this.$refs.orderDelivery.close();
						} else {
							this.isRepeat = false;
							this.$util.showToast({
								title: res.message
							});
						}
					}
				});
			}
		}
	};

	/**
	 * 打印回调
	 * @param {Object} text
	 */
	window.POS_PRINT_CALLBACK = function(text) {
		uni.showToast({
			title: text,
			icon: 'none'
		})
	}
</script>

<style scoped lang="scss">
	@import './public/css/orderlist.scss';

	.tab-wrap {
		padding: 0 !important;
		background-color: #fff !important;

		.tab-head {
			display: flex;
			background-color: #f8f8f8;

			text {
				width: 1.15rem;
				height: 0.55rem;
				line-height: 0.55rem;
				text-align: center;
				font-size: $uni-font-size-lg;

				&.active {
					background-color: #fff;
				}
			}
		}
	}

	.item-box {
		padding: 0.1rem;
	}

	.form-radio-item {
		margin-right: .1rem;
		display: inline-flex;
		align-items: center;
	}
</style>